package edu.whu.puppygo.exception;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public final class BackendException extends RuntimeException {
    /**
     * token为空时的异常
     */
    public static final BackendException AUTH_EMPTY_TOKEN = new BackendException("token为空");
    public static final BackendException ADMIN_NAME_EXISTED = new BackendException("该用户名已存在");

    public static final BackendException ADMIN_NOT_EXISTED = new BackendException("该用户不存在");
    public static final BackendException ADMIN_OLD_PASSWORD_ERROR = new BackendException("旧密码错误");
    public static final BackendException ADMIN_PASSWORD_ERROR = new BackendException("密码错误");

    /**
     * 接口异常
     */
    public static final BackendException API_DEPRECATED = new BackendException("该api已废弃");
    private final int code;

    public BackendException(String message) {
        super(message);
        this.code = -1;
    }

    public BackendException(String message, Throwable cause) {
        super(message, cause);
        this.code = -1;
    }

    public BackendException(int code) {
        this.code = code;
    }

    public BackendException(String message, int code) {
        super(message);
        this.code = code;
        logException(message);
    }

    public BackendException(String message, Throwable cause, int code) {
        super(message, cause);
        this.code = code;

        logException(message);
    }

    private static void logException(String message) {
        log.warn("Backend Exception: {}", message);
    }

    public int getCode() {
        return code;
    }

}
